package com.heer.gymnasium.module.message.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.article.views.ArticleActivity;
import com.heer.gymnasium.module.card.views.MyCardActivity;
import com.heer.gymnasium.module.fellow.views.FellowDetailActivity;
import com.heer.gymnasium.module.main.views.FindFragment;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.gymnasium.module.message.beans.MsgBean;
import com.heer.gymnasium.module.message.beans.MsgListResponseBean;
import com.heer.gymnasium.module.store.views.StoreActivity;
import com.heer.gymnasium.module.team.views.TeamDetailActivity;
import com.heer.gymnasium.util.StringUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/2/12
 */

public class MessageListActivity extends BaseRefreshActivity {

    private static final int RES_ID_LAYOUT = R.layout.layout_message_list;
    private static final int RES_ID_REFRESH = R.id.layout_message_refresh_srl;
    private static final int RES_ID_LIST = R.id.layout_message_lv;

    private static final int RES_ID_ITEM = R.layout.item_message;
    private static final int RES_ID_ITEM_TIME = R.id.item_message_time_tv;
    private static final int RES_ID_ITEM_TITLE = R.id.item_message_noticetitle_tv;
    private static final int RES_ID_ITEM_CONTENT = R.id.item_message_noticeconten_tv;
    private static final int RES_ID_ITEM_LOOK = R.id.item_message_look_tv;

    //    1:系统推送，2:拼单成功，3:约战成功，4:拼单失败，5:约战失败，6:卡券到期，7:入场提醒，8:推荐拼单 9:系统推送店铺，10:系统推送文章
    private static final String TYPE_SYS = "1", FELLOW_SUCCESS = "2", MATCH_SUCCESS = "3",
            FELLOW_FAIL = "4", MATCH_FAIL = "5", CARD_COUPON = "6", WARN = "7", RECOMMEND_FELLOW = "8", SYSTEM_STADIUM = "9", SYSTEM_ARTICLE = "10";
    private QuickTypeAdapter<MsgBean> adapter;
    private List<MsgBean> data;
    private Activity $this;

    @BindView(RES_ID_LIST)
    ListView listView;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, MessageListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        setContentView(RES_ID_LAYOUT);
        setRefreshResId(RES_ID_REFRESH);
        $this = this;
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("消息中心");
        titleUtils.startMainActivity(this);
        data = new ArrayList<>();

        adapter = new QuickTypeAdapter<MsgBean>($this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, final MsgBean data, int type, int pos) {
                holder.setText(RES_ID_ITEM_TIME, StringUtils.getPhpDate(data.getTime()));
                holder.setText(RES_ID_ITEM_TITLE, data.getTitle());
                holder.setText(RES_ID_ITEM_CONTENT, data.getContent());
                if (data.getMsgType().equals(FELLOW_FAIL) || data.getMsgType().equals(MATCH_FAIL)) {
                    holder.setVisibility(RES_ID_ITEM_LOOK, View.GONE);
                } else {
                    holder.setVisibility(RES_ID_ITEM_LOOK, View.VISIBLE);
                }
                holder.setLis(RES_ID_ITEM_LOOK, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (data.getMsgType()) {
                            case TYPE_SYS:
                                break;
                            case FELLOW_SUCCESS:
                                MyCardActivity.startActivity($this, 2);
                                break;
                            case MATCH_SUCCESS:
                                FellowDetailActivity.startActivity(context, Type.TYPE_MESSAGE, data.getInitiateOrderId());
                                break;
                            case CARD_COUPON:
                                MyCardActivity.startActivity($this, 1);
                                break;
                            case WARN:
                                break;
                            case RECOMMEND_FELLOW:
                                FellowDetailActivity.startActivity($this, Type.TYPE_FELLOW, data.getOrderId());
                                break;
                            case SYSTEM_STADIUM:
                                StoreActivity.startActivity(context, data.getForeign_id());
                                break;
                            case SYSTEM_ARTICLE:
                                ArticleActivity.startActivity(context, data.getForeign_id(), FindFragment.ARTICLE_TYPE_ACT);
                                break;

                        }
                    }
                });
            }
        };
        listView.setAdapter(adapter);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        if (layoutRefresh.isRefreshing()) {
            layoutRefresh.setRefreshing(false);
        }
        getMsgList();
    }

    private void getMsgList() {
        mAppHelper.apiService().getMsgList(pageNo + "").enqueue(new BaseCallback<ApiResponse<MsgListResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<MsgListResponseBean>> call, Response<ApiResponse<MsgListResponseBean>> response) {
                int total = StringUtils.toInt(response.body().getData().getTotal());
                int current = StringUtils.toInt(response.body().getData().getCurrent_page());
                int per_page = StringUtils.toInt(response.body().getData().getPer_page());
                if (total <= current * per_page) {
                    layoutRefresh.setDirection(SwipyRefreshLayoutDirection.TOP);
                } else {
                    layoutRefresh.setDirection(SwipyRefreshLayoutDirection.BOTTOM);
                }
                if (pageNo == 1) {
                    data.clear();
                }
                data.addAll(MsgBean.dataChange(response.body().getData().getData()));
                adapter.notifyDataSetChanged();
            }
        });
    }
}
